<?php

class manage_customer_controller{
    
    
    public function exec_index(){ 
       $content=$this->show_index();
       return array('content'=>$content);
    }
    public function exec_create(){ 
       $content=$this->show_create();
       return array('content'=>$content);
    }
    
    public function exec_edit(){
       $content=$this->show_edit();
       return array('content'=>$content);
    }
    
   public function exec_view(){
       $content=$this->show_view_detail();
       return array('content'=>$content);
    }
    
   public function exec_delete(){
      $c_id = (int) GET_value('c_id',0);

        $r = $this->delete_customer($c_id); 
        if ($r['success']){
            $content = '<div class="success">Delete customer successful!</div>';
            $index = $this->exec_index();
            $content.=$index['content']; 
        }else{
            $content='';
            if (isset($r['error'])) $content .= '<div class="error">Something wrong! '.$r['error'].'</div>';
            $index = $this->exec_index();
            $content .= $index['content'];
        }
        return array('content' => $content);
  }
  
  
   private function show_view_detail(){
       global $dbdefault;
       $c_id =  GET_value('c_id','');
       // config lien quan
       $qryconfig=sprintf("SELECT idconfigurations, configname FROM configurations WHERE idcustomer=%d",  qs($c_id));
       $resqry = mysql_query($qryconfig,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
        $postsconfig=array();
        while($arr = mysql_fetch_assoc($resqry)){
            $postsconfig[] = $arr;            
        }
       
       // device lien quan
       $qrydevice=sprintf("SELECT iddevices, namedevice FROM devices WHERE idcustomer=%d and status=1",  qs($c_id));
       $resdevice = mysql_query($qrydevice,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
        $postsdevice=array();
        while($arr = mysql_fetch_assoc($resdevice)){
            $postsdevice[] = $arr;            
        }
       //thong tin kh
       $query=  sprintf("SELECT a.*, b.user_name FROM customers a JOIN users b ON a.`user_create`=b.`user_id` 
           WHERE a.idcustomers=%d",  qs($c_id));
        //echo $query;
       $res = mysql_query($query,$dbdefault);
       $row = mysql_fetch_array($res);  
       
       $posts_tpl = new Template(MODULE_ROOT.'/manage_customer/view/view_detail.html');
        $posts_tpl ->set('row', $row);
        $posts_tpl ->set('postsconfig', $postsconfig);
        $posts_tpl ->set('postsdevice', $postsdevice);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_customer/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
   } 
    
    private function show_edit(){
        global $dbdefault;
        $c_id =  GET_value('c_id','');
        $query=  sprintf("select * from customers where idcustomers=%d",  qs($c_id));
        //echo $query;
        $res = mysql_query($query,$dbdefault);
        $row = mysql_fetch_array($res);   
        //print_r($row);
        $posts_tpl = new Template(MODULE_ROOT.'/manage_customer/view/edit_form.html');
        $posts_tpl ->set('row', $row);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_customer/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);
        return $tpl ->fetch(); 
    }
    
    private function show_create(){
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_customer/view/create_form.html');
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_customer/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);
        return $tpl ->fetch(); 
    }
    
    private function show_index(){
        global $dbdefault;
        
        $s =  GET_value('s','');
        $item_per_page = 20;        
        $page_num = GET_value('p',1);
        $limit = ($page_num-1)*$item_per_page;
        $offset = $item_per_page+1;
        
        if($s==''){
            $qry="SELECT count(*) AS total FROM customers"; 
            $query = "SELECT * FROM customers order by namecustomer LIMIT $limit, $offset";
        }else{
            $qry="SELECT count(*) AS total FROM customers where namecustomer like '%$s%'"; 
            $query = "SELECT * FROM customers where namecustomer like '%$s%' order by namecustomer LIMIT $limit, $offset";
        }
        //$qry="SELECT count(*) AS total FROM customers"; 
        
        $res_total=mysql_query($qry,$dbdefault) or die(mysql_error()."<br />".$qry);
        $total=mysql_result($res_total,0,'total');
        $num_pages=ceil($total/$item_per_page);
        
        
        //$query = "SELECT  * FROM customers ORDER BY namecustomer DESC LIMIT $limit, $offset";
        $res = mysql_query($query,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
        $posts=array();
        while($arr = mysql_fetch_assoc($res)){
            $posts[] = $arr;            
        }
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_customer/view/view_index.html');
        $posts_tpl ->set('posts', $posts);
        $posts_tpl->set('page_num', $page_num);
        $posts_tpl->set('num_pages', $num_pages);
        $posts_tpl->set('s', $s);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_customer/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);
        //$tpl ->set('m', $m);
        //print_r($m);
        return $tpl ->fetch(); 
    }
    
    
   private function show_left_menu($action=''){
        
        if (empty($action)) $action =  GET_value('a','index');
        $tpl = new Template(MODULE_ROOT.'/manage_customer/view/left_menu.html');
        $tpl->set('a',$action);
        return $tpl->fetch();
    }
    
    //-----------------------------------AJAX--------------------------------------------------
    public function ajax_create(){
        global $dbdefault;
        $c_name = GET_value('c_name');
        $c_detail = GET_value('c_detail');
        $c_address = GET_value('c_address');
        $c_phone = GET_value('c_phone');
        $c_service = GET_value('c_service');
        $service = GET_value('service');
        $equipment = GET_value('equipment');

        $user_id=$_SESSION['user_id'];
        
        $query=  sprintf("insert into customers (namecustomer, details, address, phone, service_agreement,
            service_status, project_status, time_create, user_create) 
            values(%s,%s,%s,%s,%s,%d,%d,NOW(),%d)",  qs($c_name),  qs($c_detail),  qs($c_address),  qs($c_phone),
            qs($c_service),qs($service),  qs($equipment),  qs($user_id));
        
        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            return array('success'=>TRUE);
        }
    }
    
    
    public function ajax_update(){
        global $dbdefault;
        $c_name = GET_value('c_name');
        $c_detail = GET_value('c_detail');
        $c_address = GET_value('c_address');
        $c_phone = GET_value('c_phone');
        $c_service = GET_value('c_service');
        $service = GET_value('service');
        $equipment = GET_value('equipment');
        $idcustomer = GET_value('idcustomer');

        $user_id=$_SESSION['user_id'];
        
        $query=  sprintf("update customers set namecustomer=%s, details=%s, address=%s, phone=%s, service_agreement=%s,
            service_status=%d, project_status=%d, time_update=NOW() where idcustomers=%d",  qs($c_name),  qs($c_detail),  qs($c_address),  qs($c_phone),
            qs($c_service),qs($service),  qs($equipment),  qs($idcustomer));
        
        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            return array('success'=>TRUE);
        }
    }
    
    private function delete_customer($c_id){
        global $dbdefault;
        $query="DELETE from customers WHERE idcustomers=$c_id";

        if($c_id==1){
            return array('success'=>FALSE,'error'=> "Can not delete service owner");
        }
        else {
            $result=@mysql_query($query,$dbdefault);
            if (!$result){
                echo mysql_error();
                return array('success'=>FALSE,'error'=> mysql_error());
            }else{
                return array('success'=>TRUE,'content'=>" is deleted");
            }
        }
    }
}
?>
